const cache = new Cache(config.timeout * 3)
const data = [0, 0, 0, 0, 0]

async function main(market, base, quote) {

	const url = `https://poloniex.com/public?command=returnOrderBook&currencyPair=${quote.toUpperCase()}_${base.toUpperCase()}&depth=10`
	try {
		const {statusCode, body} = await Request.getAsync(url, {timeout: config.timeout, json: true})
		// console.log(url, statusCode, body)

		if (statusCode == 200 && body.asks) {
			data[1] = Math.min(...body.asks.map(item => item[0]))
			data[2] = Math.max(...body.bids.map(item => item[0]))
			data[3] = Math.max(...body.bids.map(item => item[1]))
		    data[4] = Math.min(...body.asks.map(item => item[1]))
			cache.put('data', data)
		}
	} catch (error) {
		LOG([market, error.code || error.message || error])
	}

	await Promise.delay(config.timeout * 0.5)
	main(market, base, quote)
}

exports.init = main
exports.data = cache